package com.ywjh.inter.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;

import com.ywjh.inter.common.MemKey;
import com.ywjh.inter.conn.DBConnection;
import com.ywjh.inter.conn.MemcacheConn;
import com.ywjh.inter.dao.LocalDAO;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class DocService {
	private static DocService instance;
	private JSONArray location = new JSONArray();
	private LocalDAO docDAO;


	public static DocService getInstance() {
		if (instance == null) {
			instance = new DocService();
			instance.docDAO=new LocalDAO();
			instance.initStatic();
		}
		return instance;
	}
	
	public JSONArray getLocation() {
		return location;
	}

	private void initStatic() {
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			Map<String, String> pm = docDAO.getP(conn);
			for (String id : pm.keySet()) {
				Map<String, String> cityMap = docDAO.findCityByPid(conn, Integer.parseInt(id));
				JSONObject d = new JSONObject();
				d.put("id", id);
				d.put("name", pm.get(id));
				JSONArray cityArray = new JSONArray();
				for (String cid : cityMap.keySet()) {
					JSONObject cj = new JSONObject();
					JSONArray localArray = new JSONArray();
					cj.put("id", cid);
					cj.put("name", cityMap.get(cid));
					Map<String, String> localMap = docDAO.findLocalbyCid(conn, Integer.parseInt(cid));
					for (String lid : localMap.keySet()) {
						JSONObject lj = new JSONObject();
						lj.put("id", lid);
						lj.put("name", localMap.get(lid));
						localArray.add(lj);
					}
					cj.put("local_array", localArray);
					cityArray.add(cj);
				}
				d.put("city_array", cityArray);
				location.add(d);

			}
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}

	}

	public JSONArray getDisease() {
		String str = MemcacheConn.getInstance().get(MemKey.disease);
		if (str != null) {
			return JSONArray.fromObject(str);
		}
		return null;
	}
	
	public JSONArray getBGluPlan() {
		String str = MemcacheConn.getInstance().get(MemKey.bglu_plan);
		if (str != null) {
			return JSONArray.fromObject(str);
		}
		return null;
	}
	

	public JSONArray getHisDisease() {
		String str = MemcacheConn.getInstance().get(MemKey.his_disease);
		if (str != null) {
			return JSONArray.fromObject(str);
		}
		return null;
	}

	public JSONArray getMedicine() {
		String str = MemcacheConn.getInstance().get(MemKey.medicine);
		if (str != null) {
			return JSONArray.fromObject(str);
		}
		return null;
	}

	

	public String getDiseaseName(Integer key) {
		JSONArray array=getDisease();
		for(int i=0;i<array.size();i++) {
			JSONObject js=array.getJSONObject(i);
			if(js.getInt("id")==key) {
				return js.getString("name");
			}
		}
		return null;
	}
	
	
	public JSONArray getGroup() {
		String str = MemcacheConn.getInstance().get(MemKey.group);
		if (str != null) {
			return JSONArray.fromObject(str);
		}
		return null;
	}
	public JSONArray getGroupDis() {
		String str = MemcacheConn.getInstance().get(MemKey.group_disease);
		if (str != null) {
			return JSONArray.fromObject(str);
		}
		return null;
	}

	

}
